<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>npm中常用命令与模块 | 小熊的技术文档</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/fav.ico">
    <link rel="stylesheet" href="/css/katex.min.css">
    <link rel="stylesheet" href="/css/github-markdown.min.css">
    <meta name="description" content="衣带渐宽终不悔，为伊消得人憔悴">
    
    <link rel="preload" href="/assets/css/0.styles.e6449353.css" as="style"><link rel="preload" href="/assets/js/app.9e067b79.js" as="script"><link rel="preload" href="/assets/js/2.983eb755.js" as="script"><link rel="preload" href="/assets/js/26.6da7ea48.js" as="script"><link rel="prefetch" href="/assets/js/10.79548333.js"><link rel="prefetch" href="/assets/js/11.31600f80.js"><link rel="prefetch" href="/assets/js/12.62bd3528.js"><link rel="prefetch" href="/assets/js/13.de8b1ace.js"><link rel="prefetch" href="/assets/js/14.f5db1e75.js"><link rel="prefetch" href="/assets/js/15.bf827d4d.js"><link rel="prefetch" href="/assets/js/16.cc9a1a73.js"><link rel="prefetch" href="/assets/js/17.2cfaefeb.js"><link rel="prefetch" href="/assets/js/18.3af7782b.js"><link rel="prefetch" href="/assets/js/19.639f7e7b.js"><link rel="prefetch" href="/assets/js/20.dc862f59.js"><link rel="prefetch" href="/assets/js/21.916e1685.js"><link rel="prefetch" href="/assets/js/22.d7a955f4.js"><link rel="prefetch" href="/assets/js/23.078391ef.js"><link rel="prefetch" href="/assets/js/24.bb460a5a.js"><link rel="prefetch" href="/assets/js/25.d69f2326.js"><link rel="prefetch" href="/assets/js/27.82e23d91.js"><link rel="prefetch" href="/assets/js/28.9073bbec.js"><link rel="prefetch" href="/assets/js/29.639259a4.js"><link rel="prefetch" href="/assets/js/3.e594e5b2.js"><link rel="prefetch" href="/assets/js/30.b49c622d.js"><link rel="prefetch" href="/assets/js/31.92f6c8b3.js"><link rel="prefetch" href="/assets/js/32.42419088.js"><link rel="prefetch" href="/assets/js/33.c82e2ab8.js"><link rel="prefetch" href="/assets/js/34.381de37e.js"><link rel="prefetch" href="/assets/js/35.5e86d478.js"><link rel="prefetch" href="/assets/js/36.31f218ce.js"><link rel="prefetch" href="/assets/js/37.0d287b3f.js"><link rel="prefetch" href="/assets/js/38.1324cf44.js"><link rel="prefetch" href="/assets/js/39.de6bdb51.js"><link rel="prefetch" href="/assets/js/4.440c4dd9.js"><link rel="prefetch" href="/assets/js/40.a22c9c27.js"><link rel="prefetch" href="/assets/js/41.4637d617.js"><link rel="prefetch" href="/assets/js/42.db815e2b.js"><link rel="prefetch" href="/assets/js/43.f0955a92.js"><link rel="prefetch" href="/assets/js/44.7d5faddf.js"><link rel="prefetch" href="/assets/js/45.a88ffc33.js"><link rel="prefetch" href="/assets/js/46.883caa71.js"><link rel="prefetch" href="/assets/js/47.6f2cfd60.js"><link rel="prefetch" href="/assets/js/48.ea25654e.js"><link rel="prefetch" href="/assets/js/49.f38c23a0.js"><link rel="prefetch" href="/assets/js/5.e8844f36.js"><link rel="prefetch" href="/assets/js/50.e03dd8e9.js"><link rel="prefetch" href="/assets/js/51.f6160d52.js"><link rel="prefetch" href="/assets/js/52.4daa8322.js"><link rel="prefetch" href="/assets/js/53.b30992e9.js"><link rel="prefetch" href="/assets/js/54.209f17e1.js"><link rel="prefetch" href="/assets/js/55.4f1dd95b.js"><link rel="prefetch" href="/assets/js/56.147ea3e8.js"><link rel="prefetch" href="/assets/js/57.5823e0e2.js"><link rel="prefetch" href="/assets/js/58.772320f9.js"><link rel="prefetch" href="/assets/js/59.5ab55a80.js"><link rel="prefetch" href="/assets/js/6.54e1cc95.js"><link rel="prefetch" href="/assets/js/60.b47b75bb.js"><link rel="prefetch" href="/assets/js/61.0cd5a012.js"><link rel="prefetch" href="/assets/js/62.35eb5453.js"><link rel="prefetch" href="/assets/js/63.d31f2118.js"><link rel="prefetch" href="/assets/js/64.03d35d7c.js"><link rel="prefetch" href="/assets/js/65.8bdc633f.js"><link rel="prefetch" href="/assets/js/66.dbbe8867.js"><link rel="prefetch" href="/assets/js/67.228613b5.js"><link rel="prefetch" href="/assets/js/68.d10a2687.js"><link rel="prefetch" href="/assets/js/69.7903847f.js"><link rel="prefetch" href="/assets/js/7.f5ab00eb.js"><link rel="prefetch" href="/assets/js/70.11ee4e27.js"><link rel="prefetch" href="/assets/js/8.95b39934.js"><link rel="prefetch" href="/assets/js/9.26cfd48c.js">
    <link rel="stylesheet" href="/assets/css/0.styles.e6449353.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">小熊的技术文档</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/front-end/" class="nav-link router-link-active">
  🎨前端
</a></div><div class="nav-item"><a href="/back-end/" class="nav-link">
  💻后端
</a></div><div class="nav-item"><a href="/practice/" class="nav-link">
  🚀实战
</a></div><div class="nav-item"><a href="/office/" class="nav-link">
  🏢办公
</a></div><div class="nav-item"><a href="/general/" class="nav-link">
  🍓通用
</a></div><div class="nav-item"><a href="/paper/" class="nav-link">
  🐸论文
</a></div><div class="nav-item"><a href="/general/fast.html" class="nav-link">
  ⚡快速笔记
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="🦉近期重点" class="dropdown-title"><span class="title">🦉近期重点</span> <span class="arrow down"></span></button> <button type="button" aria-label="🦉近期重点" class="mobile-dropdown-title"><span class="title">🦉近期重点</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/back-end/python.html" class="nav-link">
  🐇python常用模块
</a></li><li class="dropdown-item"><!----> <a href="/practice/zrender.html" class="nav-link">
  🌹zrender源码解析
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="⭐️资源" class="dropdown-title"><span class="title">⭐️资源</span> <span class="arrow down"></span></button> <button type="button" aria-label="⭐️资源" class="mobile-dropdown-title"><span class="title">⭐️资源</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://www.birdiesearch.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  小鸟搜索
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://salttiger.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  每天一本编程书
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/docmirror/dev-sidecar" target="_blank" rel="noopener noreferrer" class="nav-link external">
  开发者边车
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/front-end/" class="nav-link router-link-active">
  🎨前端
</a></div><div class="nav-item"><a href="/back-end/" class="nav-link">
  💻后端
</a></div><div class="nav-item"><a href="/practice/" class="nav-link">
  🚀实战
</a></div><div class="nav-item"><a href="/office/" class="nav-link">
  🏢办公
</a></div><div class="nav-item"><a href="/general/" class="nav-link">
  🍓通用
</a></div><div class="nav-item"><a href="/paper/" class="nav-link">
  🐸论文
</a></div><div class="nav-item"><a href="/general/fast.html" class="nav-link">
  ⚡快速笔记
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="🦉近期重点" class="dropdown-title"><span class="title">🦉近期重点</span> <span class="arrow down"></span></button> <button type="button" aria-label="🦉近期重点" class="mobile-dropdown-title"><span class="title">🦉近期重点</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/back-end/python.html" class="nav-link">
  🐇python常用模块
</a></li><li class="dropdown-item"><!----> <a href="/practice/zrender.html" class="nav-link">
  🌹zrender源码解析
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="⭐️资源" class="dropdown-title"><span class="title">⭐️资源</span> <span class="arrow down"></span></button> <button type="button" aria-label="⭐️资源" class="mobile-dropdown-title"><span class="title">⭐️资源</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://www.birdiesearch.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  小鸟搜索
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://salttiger.com/" target="_blank" rel="noopener noreferrer" class="nav-link external">
  每天一本编程书
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/docmirror/dev-sidecar" target="_blank" rel="noopener noreferrer" class="nav-link external">
  开发者边车
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><a href="/front-end/javascript.html" class="sidebar-link">Javascript/ECMAScript 相关技术</a></li><li><a href="/front-end/pattern.html" class="sidebar-link">前端设计模式</a></li><li><a href="/front-end/typescript.html" class="sidebar-link">学习 Typescript 实战开发</a></li><li><a href="/front-end/es6.html" class="sidebar-link">深入理解ES6</a></li><li><a href="/front-end/css.html" class="sidebar-link">css 实战学习</a></li><li><a href="/front-end/node.html" class="sidebar-link">Node.js深入浅出</a></li><li><a href="/front-end/design.html" class="sidebar-link">前端设计与可视化</a></li><li><a href="/front-end/npm.html" aria-current="page" class="active sidebar-link">npm中常用命令与模块</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/front-end/npm.html#npm常用命令" class="sidebar-link">npm常用命令</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/front-end/npm.html#配置命令" class="sidebar-link">配置命令</a></li><li class="sidebar-sub-header"><a href="/front-end/npm.html#模块命令" class="sidebar-link">模块命令</a></li><li class="sidebar-sub-header"><a href="/front-end/npm.html#查看路径" class="sidebar-link">查看路径</a></li><li class="sidebar-sub-header"><a href="/front-end/npm.html#运行脚本" class="sidebar-link">运行脚本</a></li></ul></li><li class="sidebar-sub-header"><a href="/front-end/npm.html#npm常用模块" class="sidebar-link">npm常用模块</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/front-end/npm.html#命令行参数解析模块" class="sidebar-link">命令行参数解析模块</a></li></ul></li><li class="sidebar-sub-header"><a href="/front-end/npm.html#npm安装时碰到的问题" class="sidebar-link">npm安装时碰到的问题</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/front-end/npm.html#安装electron失败的解决方案" class="sidebar-link">安装Electron失败的解决方案</a></li></ul></li></ul></li><li><a href="/front-end/vuepress.html" class="sidebar-link">vuepress 基本配置</a></li><li><a href="/front-end/babel.html" class="sidebar-link">Babel详细学习</a></li><li><a href="/front-end/vue.html" class="sidebar-link">Vue项目实战</a></li><li><a href="/front-end/react.html" class="sidebar-link">React17+React Hook+TS4 最佳实践</a></li><li><a href="/front-end/electron.html" class="sidebar-link">electron实战</a></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="npm中常用命令与模块"><a href="#npm中常用命令与模块" class="header-anchor">#</a> npm中常用命令与模块</h1> <h2 id="npm常用命令"><a href="#npm常用命令" class="header-anchor">#</a> npm常用命令</h2> <h3 id="配置命令"><a href="#配置命令" class="header-anchor">#</a> 配置命令</h3> <div class="language-shell extra-class"><pre class="language-shell"><code><span class="token comment"># 查看registry</span>
<span class="token function">npm</span> config get registry

<span class="token comment"># 修改为淘宝镜像</span>
<span class="token function">npm</span> config <span class="token builtin class-name">set</span> registry http://registry.npm.taobao.org

<span class="token comment"># 修改为本地Nexus服务器镜像</span>
<span class="token function">npm</span> config <span class="token builtin class-name">set</span> registry http://localhost:8081/repository/npm-proxy/

<span class="token comment"># 修改为原始镜像</span>
<span class="token function">npm</span> config <span class="token builtin class-name">set</span> registry http://registry.npmjs.org

<span class="token comment"># 查看所有配置</span>
<span class="token function">npm</span> config list
<span class="token function">npm</span> config list --json  <span class="token comment"># 以json方式查看</span>

<span class="token comment"># 完整版语法</span>
<span class="token function">npm</span> config <span class="token builtin class-name">set</span> <span class="token operator">&lt;</span>key<span class="token operator">&gt;</span> <span class="token operator">&lt;</span>value<span class="token operator">&gt;</span> <span class="token punctuation">[</span>-g<span class="token operator">|</span>--global<span class="token punctuation">]</span>
<span class="token function">npm</span> config get <span class="token operator">&lt;</span>key<span class="token operator">&gt;</span>
<span class="token function">npm</span> config delete <span class="token operator">&lt;</span>key<span class="token operator">&gt;</span>
<span class="token function">npm</span> config list
<span class="token function">npm</span> config edit
<span class="token function">npm</span> get <span class="token operator">&lt;</span>key<span class="token operator">&gt;</span>
<span class="token function">npm</span> <span class="token builtin class-name">set</span> <span class="token operator">&lt;</span>key<span class="token operator">&gt;</span> <span class="token operator">&lt;</span>value<span class="token operator">&gt;</span> <span class="token punctuation">[</span>-g<span class="token operator">|</span>--global<span class="token punctuation">]</span>
</code></pre></div><h3 id="模块命令"><a href="#模块命令" class="header-anchor">#</a> 模块命令</h3> <div class="language-shell extra-class"><pre class="language-shell"><code><span class="token comment"># 初始化package.json</span>
<span class="token function">npm</span> init <span class="token punctuation">[</span>-f<span class="token operator">|</span>--force<span class="token operator">|</span>-y<span class="token operator">|</span>--yes<span class="token punctuation">]</span>

<span class="token comment"># 安装模块</span>
<span class="token function">npm</span> <span class="token function">install</span>  <span class="token comment"># 不加任何参数则安装package.json中所有依赖包</span>
<span class="token function">npm</span> <span class="token function">install</span> <span class="token punctuation">[</span><span class="token operator">&lt;</span>@scope<span class="token operator">&gt;</span>/<span class="token punctuation">]</span><span class="token operator">&lt;</span>name<span class="token operator">&gt;</span>
<span class="token function">npm</span> <span class="token function">install</span> <span class="token punctuation">[</span><span class="token operator">&lt;</span>@scope<span class="token operator">&gt;</span>/<span class="token punctuation">]</span><span class="token operator">&lt;</span>name<span class="token operator">&gt;</span>@<span class="token operator">&lt;</span>tag<span class="token operator">&gt;</span>
<span class="token function">npm</span> <span class="token function">install</span> <span class="token punctuation">[</span><span class="token operator">&lt;</span>@scope<span class="token operator">&gt;</span>/<span class="token punctuation">]</span><span class="token operator">&lt;</span>name<span class="token operator">&gt;</span>@<span class="token operator">&lt;</span>version<span class="token operator">&gt;</span>
<span class="token function">npm</span> <span class="token function">install</span> <span class="token punctuation">[</span><span class="token operator">&lt;</span>@scope<span class="token operator">&gt;</span>/<span class="token punctuation">]</span><span class="token operator">&lt;</span>name<span class="token operator">&gt;</span>@<span class="token operator">&lt;</span>version range<span class="token operator">&gt;</span>
<span class="token function">npm</span> <span class="token function">install</span> <span class="token operator">&lt;</span>tarball file<span class="token operator">&gt;</span>
<span class="token function">npm</span> <span class="token function">install</span> <span class="token operator">&lt;</span>tarball url<span class="token operator">&gt;</span>
<span class="token function">npm</span> <span class="token function">install</span> <span class="token operator">&lt;</span>folder<span class="token operator">&gt;</span>
<span class="token comment"># 别名</span>
<span class="token function">npm</span> i
<span class="token comment"># 可选配置</span>
<span class="token punctuation">[</span>-S<span class="token operator">|</span>--save<span class="token operator">|</span>-D<span class="token operator">|</span>--save-dev<span class="token operator">|</span>-O<span class="token operator">|</span>--save-optional<span class="token punctuation">]</span> <span class="token punctuation">[</span>-E<span class="token operator">|</span>--save-exact<span class="token punctuation">]</span> <span class="token punctuation">[</span>--dry-run<span class="token punctuation">]</span>

<span class="token comment"># 卸载模块</span>
<span class="token function">npm</span> uninstall <span class="token punctuation">[</span><span class="token operator">&lt;</span>@scope<span class="token operator">&gt;</span>/<span class="token punctuation">]</span><span class="token operator">&lt;</span>pkg<span class="token operator">&gt;</span><span class="token punctuation">[</span>@<span class="token operator">&lt;</span>version<span class="token operator">&gt;</span><span class="token punctuation">]</span><span class="token punctuation">..</span>. <span class="token punctuation">[</span>-S<span class="token operator">|</span>--save<span class="token operator">|</span>-D<span class="token operator">|</span>--save-dev<span class="token operator">|</span>-O<span class="token operator">|</span>--save-optional<span class="token punctuation">]</span>

<span class="token comment"># 更新模块</span>
<span class="token function">npm</span> update <span class="token punctuation">[</span>-g<span class="token punctuation">]</span> <span class="token punctuation">[</span><span class="token operator">&lt;</span>pkg<span class="token operator">&gt;</span><span class="token punctuation">..</span>.<span class="token punctuation">]</span>

<span class="token comment"># 查看已安装模块</span>
<span class="token function">npm</span> list  <span class="token comment"># 查看当前目录下安装模块</span>
<span class="token function">npm</span> list -g <span class="token comment"># 查看全局安装模块</span>
<span class="token function">npm</span> list --depth<span class="token operator">=</span><span class="token number">0</span>  <span class="token comment"># 限制查看层级为0</span>
<span class="token comment"># 别名</span>
<span class="token function">npm</span> <span class="token function">ls</span>

<span class="token comment"># 查看模块版本</span>
<span class="token function">npm</span> version

</code></pre></div><h3 id="查看路径"><a href="#查看路径" class="header-anchor">#</a> 查看路径</h3> <div class="language-shell extra-class"><pre class="language-shell"><code><span class="token comment"># 查看npm安装路径</span>
<span class="token function">npm</span> get prefix

<span class="token comment"># 查看全局node包路径</span>
<span class="token function">npm</span> root -g

<span class="token comment"># npm清理缓存</span>
<span class="token function">npm</span> cache clean -f
</code></pre></div><h3 id="运行脚本"><a href="#运行脚本" class="header-anchor">#</a> 运行脚本</h3> <div class="language-shell extra-class"><pre class="language-shell"><code><span class="token comment"># 运行package.json中[Scripts]属性中的脚本</span>
<span class="token function">npm</span> run <span class="token operator">&lt;</span>脚本名称<span class="token operator">&gt;</span>
</code></pre></div><h2 id="npm常用模块"><a href="#npm常用模块" class="header-anchor">#</a> npm常用模块</h2> <h3 id="命令行参数解析模块"><a href="#命令行参数解析模块" class="header-anchor">#</a> 命令行参数解析模块</h3> <p>模块<a href="https://www.npmjs.com/package/optimist" target="_blank" rel="noopener noreferrer">optimist<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>可以方便的解析命令行传入的参数，不过目前已经==弃用==，使用方法如下：</p> <div class="language-js extra-class"><pre class="language-js"><code><span class="token keyword">var</span> argv <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'optimist'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>argv<span class="token punctuation">;</span>
 
<span class="token keyword">if</span> <span class="token punctuation">(</span>argv<span class="token punctuation">.</span>rif <span class="token operator">-</span> <span class="token number">5</span> <span class="token operator">*</span> argv<span class="token punctuation">.</span>xup <span class="token operator">&gt;</span> <span class="token number">7.138</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'Buy more riffiwobbles'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">else</span> <span class="token punctuation">{</span>
    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">'Sell the xupptumblers'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre></div><p>命令行参数传入：</p> <div class="language-batch extra-class"><pre class="language-batch"><code>$ ./xup.js --rif=55 --xup=9.52
<span class="token command"><span class="token keyword">Buy</span> more riffiwobbles</span>

$ ./xup.js --rif 12 --xup 8.1
<span class="token command"><span class="token keyword">Sell</span> the xupptumblers</span>
</code></pre></div><h2 id="npm安装时碰到的问题"><a href="#npm安装时碰到的问题" class="header-anchor">#</a> npm安装时碰到的问题</h2> <h3 id="安装electron失败的解决方案"><a href="#安装electron失败的解决方案" class="header-anchor">#</a> 安装Electron失败的解决方案</h3> <p>方法1：尝试打开项目的<code>\node_modules\electron</code>目录，然后运行<code>node install.js</code>，看能否安装成功，如果安装不成功，一般是下载文件出现问题，如下图所示。</p> <p>通过查看<code>package.json</code>，确定<code>electron</code>的版本，然后在<a href="https://npm.taobao.org/mirrors/electron/" target="_blank" rel="noopener noreferrer">淘宝electron镜像站<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a>下载对应的zip文件，例如<code>electron-v14.0.0-win32-x64.zip</code>，将其复制到项目的<code>\node_modules\electron</code>目录，然后把<code>install.js</code>改为下面代码：</p> <div class="language-js extra-class"><pre class="language-js"><code>#<span class="token operator">!</span><span class="token operator">/</span>usr<span class="token operator">/</span>bin<span class="token operator">/</span>env node

<span class="token keyword">const</span> version <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'./package'</span><span class="token punctuation">)</span><span class="token punctuation">.</span>version
<span class="token keyword">const</span> fs <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'fs'</span><span class="token punctuation">)</span>
<span class="token keyword">const</span> os <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'os'</span><span class="token punctuation">)</span>
<span class="token keyword">const</span> path <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'path'</span><span class="token punctuation">)</span>
<span class="token keyword">const</span> extract <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'extract-zip'</span><span class="token punctuation">)</span>
<span class="token keyword">const</span> platformPath <span class="token operator">=</span> <span class="token string">'electron.exe'</span>
<span class="token keyword">const</span> zipPath <span class="token operator">=</span> <span class="token string">&quot;./electron-v14.0.0-win32-x64.zip&quot;</span>
<span class="token function">extractFile</span> <span class="token punctuation">(</span>zipPath<span class="token punctuation">)</span>
<span class="token comment">// unzips and makes path.txt point at the correct executable</span>
<span class="token keyword">function</span> <span class="token function">extractFile</span> <span class="token punctuation">(</span><span class="token parameter">zipPath</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token function">extract</span><span class="token punctuation">(</span>zipPath<span class="token punctuation">,</span> <span class="token punctuation">{</span> dir<span class="token operator">:</span> path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span> <span class="token string">'dist'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">err</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token function">onerror</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
fs<span class="token punctuation">.</span><span class="token function">writeFile</span><span class="token punctuation">(</span>path<span class="token punctuation">.</span><span class="token function">join</span><span class="token punctuation">(</span>__dirname<span class="token punctuation">,</span> <span class="token string">'path.txt'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> platformPath<span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">err</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span>err<span class="token punctuation">)</span> <span class="token keyword">return</span> <span class="token function">onerror</span><span class="token punctuation">(</span>err<span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre></div><p>然后再运行<code>node install.js</code>即可。</p></div> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">更新于:</span> <span class="time">9/5/2021, 12:03:50 AM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/front-end/design.html" class="prev">
        前端设计与可视化
      </a></span> <span class="next"><a href="/front-end/vuepress.html">
        vuepress 基本配置
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/assets/js/app.9e067b79.js" defer></script><script src="/assets/js/2.983eb755.js" defer></script><script src="/assets/js/26.6da7ea48.js" defer></script>
  </body>
</html>
